<template>
  <div class="mail-detail-bg">
    <el-card class="mail-detail-card">
      <div class="go-back-wrapper">
        <GoBack />
        <el-button type="primary" class="reply-btn" @click="replyMail" style="margin-left: 16px"
          >写回信</el-button
        >
      </div>
      <h2 class="mail-title">{{ mail?.subject }}</h2>
      <div class="mail-meta">
        <div>
          <span class="meta-label">发件人：</span><span>{{ mail?.sender }}</span>
        </div>
        <div>
          <span class="meta-label">收件人：</span><span>{{ mail?.receiver }}</span>
        </div>
        <div>
          <span class="meta-label">抄送：</span><span>{{ mail?.cc }}</span>
        </div>
        <div>
          <span class="meta-label">时间：</span><span>{{ mail?.received_at }}</span>
        </div>
      </div>
      <el-divider />
      <div class="mail-body" v-html="mailBodyHtml"></div>
      <div v-if="mail?.attachments?.length" class="attach-area">
        <el-divider>附件</el-divider>
        <div class="attach-list">
          <el-link
            v-for="file in mail.attachments"
            :key="file.id"
            @click="download(file)"
            class="attach-link"
            :underline="false"
          >
            <el-icon style="vertical-align: middle; color: #d16262; margin-right: 4px"
              ><Paperclip
            /></el-icon>
            {{ file.file_name }}
          </el-link>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getMailDetailAPI } from '@/api/mailsAPI'
import { downloadAttachmentAPI } from '@/api/attachAPI'
import { ElMessage } from 'element-plus'
import { Paperclip } from '@element-plus/icons-vue'
import GoBack from '@/components/utils/GoBack.vue'

const route = useRoute()
const router = useRouter()
const mail = ref<any>(null)

const fetchDetail = async () => {
  const res = await getMailDetailAPI(Number(route.params.id))
  if (res.code === 200) mail.value = res.data
}

const download = async (file: any) => {
  try {
    const blob = await downloadAttachmentAPI(file.id)
    const url = window.URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = file.file_name
    a.click()
    window.URL.revokeObjectURL(url)
  } catch (e) {
    ElMessage.error('下载失败' + e)
  }
}

const replyMail = () => {
  if (!mail.value) return
  router.push({
    path: '/compose',
    query: {
      receiver: mail.value.sender,
      subject: mail.value.subject,
      originBody: mail.value.body,
      originSender: mail.value.sender,
      originTime: mail.value.received_at,
    },
  })
}

const mailBodyHtml = computed(() =>
  mail.value?.body ? mail.value.body.replace(/\n/g, '<br>') : '',
)

onMounted(fetchDetail)
</script>

<style scoped>
.mail-detail-bg {
  min-height: 100vh;
  background: #ffe4e1;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 40px 0;
}
.mail-detail-card {
  width: 1700px;
  border-radius: 20px;
  box-shadow: 0 4px 24px rgba(209, 96, 83, 0.1);
  padding: 36px 48px 32px 48px;
  background: #fff;
  position: relative;
}
.go-back-wrapper {
  position: absolute;
  top: 24px;
  left: 32px;
  z-index: 2;
  display: flex;
  align-items: center;
}
.reply-btn {
  background-color: #ff8585;
  border: none;
  color: #fff;
  font-weight: bold;
  border-radius: 8px;
  padding: 6px 22px;
  font-size: 16px;
  transition: background 0.2s;
}
.reply-btn:hover {
  background-color: #d16262;
}
.mail-title {
  font-size: 26px;
  color: #d16262;
  margin-bottom: 18px;
  font-weight: bold;
  word-break: break-all;
}
.mail-meta {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  gap: 12px 24px;
  font-size: 15px;
  color: #666;
  margin-bottom: 12px;
}
.meta-label {
  color: #d16262;
  font-weight: bold;
  margin-right: 2px;
}
.mail-body {
  font-size: 16px;
  color: #222;
  line-height: 1.8;
  margin: 24px 0 0 0;
  word-break: break-all;
  min-height: 120px;
}
.attach-area {
  margin-top: 32px;
}
.attach-list {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 8px;
}
.attach-link {
  background: #fff0f0;
  border-radius: 6px;
  padding: 6px 16px;
  color: #d16262;
  font-weight: 500;
  font-size: 15px;
  transition: background 0.2s;
  cursor: pointer;
  display: flex;
  align-items: center;
}
.attach-link:hover {
  background: #ffd6d6;
  color: #b94a48;
}
</style>
